<import template="/panel/_header"/>
<div class="page-container">
    <div class="main-content">
        <div class="page-title">

            <div class="title-env">
                <h1 class="title">邀请码</h1>
                <p class="description">
                    <if condition="$user->invite_num == 0">查看自己的邀请码
                        <else>当前您可以生成<code id="invitec_num2">{$user->invite_num}</code>个邀请码。
                    </if>
                </p>
            </div>

            <div class="breadcrumb-env">
                <ol class="breadcrumb bc-1">
                    <li>
                        <a href="/member"><i class="fa-home"></i>主页</a>
                    </li>
                    <li class="active">
                        <strong>邀请码</strong>
                    </li>
                </ol>
            </div>
        </div>

        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">
                    <a class="btn btn-danger btn-sm invitec<if condition="$user->invite_num <= 0"> hidden</if>" href="javascript:;" style="color:#FFF;">生成邀请码(余 <span id="invitec_num">{$user->invite_num}</span> 次)</a>&nbsp;
                    <a class="btn btn-danger btn-sm inviteFlow" href="javascript:;" style="color:#FFF;">用流量购买邀请码生成次数</a>
                </h3>
            </div>
            <div class="panel-body">

                <table class="table table-hover middle-align">
                    <thead>
                    <tr>
                        <th>###</th>
                        <th>邀请码</th>
                        <th>状态</th>
                    </tr>
                    </thead>
                    <tbody id="data-tbody">

                    <if condition="count($inviteList) > 0">
                        <loop variable="$inviteList" key="$key" value="$invite">
                            <tr>
                                <td>
                                    {$key}
                                </td>
                                <td>
                                    {$invite->invite}
                                </td>
                                <td>
                                    <if condition="$invite->status == 0">
                                        <a href="javascript:;" class="btn btn-sm btn-secondary" data-set-skin="">可用</a>
                                        <else>
                                            <a href="javascript:;" class="btn btn-sm btn-red" data-set-skin="">已用</a>
                                    </if>
                                </td>
                            </tr>
                        </loop>
                        <else>
                            <tr>
                                <td colspan="3" align="center">您没有可用的邀请码</td>
                            </tr>

                    </tbody>
                    </if>
                </table>

                <hr>
                <h4>注意！</h4>
                <p>邀请码请给认识的需要的人。<br/>邀请码使用流量购买 1个/10GB,请不要浪费<br/>邀请有记录，若被邀请的人违反用户协议，您将会有连带责任。</p>
                <br>
                <pre>邀请码这种东西，若无力生成,请找管理员索要吧。</pre>

            </div>
        </div>
        <script>
            $(document).ready(function () {
                $(".invitec").click(function () {
                    showToastr("生成邀请码中..", 0, 2000);
                    var btn = $(this);
                    btn.attr('disabled', 'disabled');
                    $.ajax({
                        type: "GET",
                        url: "/invite/create.json",
                        dataType: "json",
                        success: function (result) {
                            if (result.code != 200) {
                                showToastr(result.message, 0, 5000, '系统错误');
                                return;
                            }
                            var data = result.data;
                            if (data.error == 0) {
                                showToastr(data.message, 0, 5000);

                                $("#invitec_num").html(data.invite_num);
                                $("#invitec_num2").html(data.invite_num);

                                if (data.invite != null) {
                                    $("#data-tbody").append('<tr><td>' + $("#data-tbody").children("tr").length + '</td><td>' + data.invite.invite + '</td><td><a href="javascript:;" class="btn btn-sm btn-secondary" data-set-skin="">可用</a></td></tr>');
                                }
                            } else {
                                showToastr(data.message, 0, 5000, '错误');
                            }
                            btn.removeAttr('disabled');
                        },
                        error: function (jqXHR) {
                            showToastr("发生错误：" + jqXHR.status, 0);
                            btn.removeAttr('disabled');
                        }
                    })
                });
                $('.inviteFlow').click(function () {
                    showToastr("正在校验信息..", 0, 1500);
                    $.ajax({
                        type: "GET",
                        url: "/invite/buy.json",
                        dataType: "json",
                        success: function (result) {
                            if (result.code != 200) {
                                showToastr(result.message, 0, 5000, '系统错误');
                                return;
                            }
                            if (result.data.error == 0) {
                                showToastr(result.data.message, 0, 5000);
                                $(".invitec").removeClass("hidden");
                                $("#invitec_num").html(result.data.invite_num);
                                $("#invitec_num2").html(result.data.invite_num);
                            } else {
                                showToastr(result.data.message, 0, 5000, '错误');
                            }
                        },
                        error: function (jqXHR) {
                            showToastr("发生错误：" + jqXHR.status, 0);
                        }
                    })
                });
            })
        </script>
        <import template="/panel/_copyright"/>
    </div>
</div>
<import template="/panel/_footer"/>